package javacommon.web.tag;

import static javacommon.util.ReflectionUtils.getter;
import static javacommon.util.ReflectionUtils.invokeMethod;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Checkbox extends SimpleTagSupport {
	private String name;//控件名称
	private Object list;//要迭代的集合或数组
	private String label;//显示文字
	private Object value;//可能是一个数组
	private String listKey;//键码
	private String listValue;//显示复选框文字
	@Override
	public void doTag() throws JspException, IOException {
		PageContext context = (PageContext) getJspContext();
		JspWriter out = context.getOut();
		if(label!=null){
			out.println(label+":");
		}
		if (list instanceof Map) {
			Map<Object, Object> m = (Map<Object, Object>) list;
			Set<Object> ks = m.keySet();
			for (Object k : ks) {
				if(value!=null&&value.getClass().isArray()){//给定的value是数组
					if(check(k,value))
						out.println("<input type='checkbox' name='"+name+"' value='"+k+"' checked='checked' />"+m.get(k));
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+k+"' />"+m.get(k));
				}else{
					if(value!=null&&(value==k||value.equals(k)))
						out.println("<input type='checkbox' name='"+name+"' value='"+k+"' checked='checked' />"+m.get(k));
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+k+"' />"+m.get(k));
				}
			}
		} else if (list.getClass().isArray()) {
			int leng = Array.getLength(list);
			for (int i = 0; i < leng; i++) {
				Object kv = Array.get(list, i);
				if(value!=null&&value.getClass().isArray()){//给定的value是数组
					if(check(kv,value))
						out.println("<input type='checkbox' name='"+name+"' value='"+kv+"' checked='checked' />"+kv);
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+kv+"' />"+kv);
				}else{
					
					if(value!=null&&(value==kv||value.equals(kv)))
						out.println("<input type='checkbox' name='"+name+"' value='"+kv+"' checked='checked' />"+kv);
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+kv+"' />"+kv);
				}
			}
		}else if(list instanceof Collection){
			Collection collection = (Collection) list;
			Iterator iter = collection.iterator();
			while(iter.hasNext()){
				Object obj = iter.next();//当前对象
				Class claz = obj.getClass();
				Object _key =invokeMethod(getter(claz, listKey), obj);
				Object _value =invokeMethod(getter(claz, listValue), obj);
				if(value!=null&&value.getClass().isArray()){//给定的value是数组
					if(check(_key,value))
						out.println("<input type='checkbox' name='"+name+"' value='"+_key+"' checked='checked' />"+_value);
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+_key+"' />"+_value);
				}else{
					
					if(value!=null&&(value==_key||value.equals(_key)))
						out.println("<input type='checkbox' name='"+name+"' value='"+_key+"' checked='checked' />"+_value);
					else
						out.println("<input type='checkbox' name='"+name+"' value='"+_key+"' />"+_value);
				}
			}
		}
	}
	private boolean check(Object k, Object value2) {
		int leng = Array.getLength(value2);
		for(int i=0;i<leng;i++){
			Object v = Array.get(value2, i);
			if(k==v||k.equals(v))
				return true;
		}
		return false;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Object getList() {
		return list;
	}
	public void setList(Object list) {
		this.list = list;
	}
	public String getLabel() {
		return label;
	}
	public void setLabel(String label) {
		this.label = label;
	}
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public String getListKey() {
		return listKey;
	}
	public void setListKey(String listKey) {
		this.listKey = listKey;
	}
	public String getListValue() {
		return listValue;
	}
	public void setListValue(String listValue) {
		this.listValue = listValue;
	}

	
}
